var primePools = [];
var primes = [];
var maxPrimes = null;
var plimit = 10000
function getPrimes(limit) {
    var sqrt = Math.floor(Math.sqrt(limit));
    primePools[2] = 0;
    primePools[1] = 1;
    for(var i=2; i<=sqrt; i++) {
        for(var j=i*2; j<=limit; j=j+i) {
            primePools[j] = 1;
        }
    }
    for(var i=2; i<=plimit; i++) {
        if(primePools[i] != 1) primes.push(i);
    }
    maxPrimes = primes[primes.length-1];
}

function isPrime(num) {
    if(num<=plimit) return primePools[num] != 1
    var sqrt=Math.floor(Math.sqrt(num));
    for(var i=0; i<=primes.length; i++) {
        if(primes[i]>sqrt) return true;
        if(num%primes[i]==0) return false;
    }
    if(sqrt>maxPrimes) {
        for(var i=maxPrimes+2; i<=sqrt+1; i++) {
            if(num%i==0) return false;
        }
        return true;
    }    
}

print('get primes.....');
var t=new Date().getTime();
getPrimes(plimit);
print('get primes done..... ' + ((new Date().getTime()-t)/1000) + "s");

function getCount(a,b) {
    var n=0, count=0, tmp=b;
    do {
        if(tmp<=0 || !isPrime(tmp)) return count;
        count++; n++;
        tmp +=a+(2*n-1);
    } while(1)
}

var maxCount = 0;
for(var a=-999; a<=999; a=a+2) {
    for(var b=2; b<1000; b++) {
        if(!isPrime(b)) continue;
        if(b<-a) continue;
        var c = getCount(a,b);
        if(c>300) {
            //print(c + "..." + a + "*" + b + "=" + a*b);
        }
        if(c>maxCount) {
            maxCount = c;
            print(maxCount + "..." + a + "*" + b + "=" + a*b);
        }
    }
    //print(a);
}
print('done..... ' + ((new Date().getTime()-t)/1000) + "s");
